<?php
namespace app\admin\controller;
use app\service\billService;
use app\service\serviceService;
use printing\printDemo;
use think\facade\Db;
use think\facade\View;

class Cashier extends Common{
    
    //权限分类
    public function cashierList(){
        //根据登录账号查询小区信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new serviceService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //查询支付类型
        $paytype = (new serviceService())->paytypeList();
        View::assign('paytypes',$paytype);
        //搜索
        $data = request()->get();
        if (!empty($data)){
            $where=array();
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['business_type'])){
                if(is_array($data['business_type'])){
                    $where['business_type'] = array('like','%'.$data['business_type'][1].'%');
                }else{
                    $where['business_type'] = array('like','%'.$data['business_type'].'%');//业务类型
                }
            }else{
                $where['business_type'] = "";
            }
            if(!empty($data['company'])){
                if(is_array($data['company'])){
                    $where['company'] = array('like','%'.$data['company'][1].'%');
                }else{
                    $where['company'] = array('like','%'.$data['company'].'%');//业务类型
                }
            }else{
                $where['company'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = $data['phone'];//联系电话
            }else{
                $where['phone'] = "";
            }
            if(!empty($data['paytype'])){
                $where['paytype'] = $data['paytype'];//支付方式
            }else{
                $where['paytype'] = "";
            }
            View::assign('where',$where);   
            $where = array_filter($where);
        }else{
            $where=array();
        }
        // dump($where);exit();
        $page = request()->has('page') ? request()->get('page') : 1;
        $cashierlist = (new serviceService())->cashierList($community_id,$page,$where);
        $number = count((new serviceService())->cashierListNumber($community_id,$where));
        View::assign('number',$number);
        if(!empty($number)){
            //根据小区编号查询小区名称
            foreach($cashierlist as $cash){
                $cash['community_id'] = (new serviceService())->communityIdName($cash['community_id']);
                $cashier[] = $cash;
            }
            View::assign('cashier',$cashier);
        }else{
            View::assign('cashier',$cashierlist);
        }
        View::assign('cashierlist',$cashierlist);
        foreach ($data as $k => $v) {
            if(is_array($v)){
                $v=str_replace('%','',$v[1]);
            }
            View::assign($k, $v);
        }
        return View::fetch('cashier_list');
    }
    public function cashierAdd(){
        //根据登录账号查询小区信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new serviceService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //查询支付类型
        $paytype = (new serviceService())->paytypeList();
        View::assign('paytype',$paytype);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            
            
            $data['time'] = date('Y-m-d H:i:s',time());
           
            //将数据插入数据库
            $result = (new serviceService())->cashierAdd($data);
            //判断数据是否插入成功
            if($result){
                 $community = (new billService())->communityMachine($data['community_id']);
                $machineCode = $community['machine_code'];//终端号
                if(!empty($machineCode)){
                    $community_name = $community['community_name'];//小区名称
                     $printing=(new printDemo())->cashierprinting($machineCode,$community_name,$data["company"],$data["business_type"],$data["paytype"],$data["money"],$data['time'],$data["phone"]);
                }
                
                return "ok";
                $this->success('添加业务收费成功',url('cashierList'));
            }else{
                return "error";
                $this->error('添加业务收费失败',url('cashierAdd'));
            }
        }else{
            return View::fetch('cashier_add');
        }
    }
    public function cashierDel(){
        $request = request();
		$id = $request->param('id');
		$validate = $this->validate($request->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
        $info = (new serviceService())->cashierId($id);
        if(empty($info)){
            $this->error('该比收银不存在');
        }
        $result = Db::name('cashier')->delete($id);
        if($result){
            $url=url('cashierList');
		    echo "<script>alert('删除成功');location.href='$url'</script>";
		    die;
			return json(['msg' => '账单删除成功']);
            $this->success('删除成功',url('cashierList'));
		}else{
            $this->success('删除失败',url('cashierList'));
		}
    }
    //导出账单数据
    public function cashierExportExcel(){
        //根据登录账号查询小区信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new serviceService())->communityList($whe);
        $community_id = $com['community_id'];
        $cashierlist = (new serviceService())->cashierListNumber($community_id,'');
        //根据小区编号查询小区名称
        foreach($cashierlist as $cash){
            $cash['community_id'] = (new serviceService())->communityIdName($cash['community_id']);
            $cashier[] = $cash;
        }
        //设置表头
        //引入PHPExcel类
        vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        $objPHPExcel = new \PHPExcel();
        //设置表头
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '小区名称')
        ->setCellValue('B1', '业务类型')
        ->setCellValue('C1', '付款单位')
        ->setCellValue('D1', '电话')
        ->setCellValue('E1', '付款金额')
        ->setCellValue('F1', '支付方式')
        ->setCellValue('G1', '付款时间');

        $i=2;  //定义一个i变量，目的是在循环输出数据是控制行数
        $count = count($cashier);  //计算有多少条数据
        for ($i = 2; $i <= $count+1; $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $cashier[$i-2]["community_id"]);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $cashier[$i-2]["business_type"]);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $cashier[$i-2]["company"]);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $cashier[$i-2]["phone"]);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $cashier[$i-2]["money"]);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $cashier[$i-2]["paytype"]);
            $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $cashier[$i-2]["time"]);
        }
        $objPHPExcel->getActiveSheet()->setTitle('收银台信息');      //设置sheet的名称
        $objPHPExcel->setActiveSheetIndex(0);                   //设置sheet的起始位置
        header('Content-Disposition: attachment;filename="收银台信息.xls"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007"); //通过PHPExcel_IOFactory的写函数将上面数据写出来
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
    }
    
    //
    public function cashierPay(){
        if(request()->isPost()){
            
        }else{
            $community_id=request()->get("community_id");
            $communityinfo=Db::table("community")->where("id",$community_id)->find();
            if(empty($communityinfo)){
                return "小区信息错误";
            }
            $cashierinfo=Db::table("cashier")->where("community_id",$community_id)->where("status","2")->find();
            if($cashierinfo){
                return View::fetch("cashier_nomsg");
            }
            return View::fetch("cashier_pay");
        }
    }
}